package it.ap.sa.acqua.plant.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import it.ap.sa.acqua.dao.beans.SearchPlantFilter;
import it.ap.sa.acqua.dao.beans.TPlantBean;
import it.ap.sa.acqua.dao.impl.TPlantDao;
import it.ap.sa.acqua.dao.impl.TPlantImageDao;
import it.ap.sa.acqua.dao.model.TPlant;
import it.ap.sa.acqua.dao.model.TPlantImage;
import it.ap.sa.core.util.IBeanPopulate;
import it.ap.sa.core.utils.CoreProperties;
import it.ap.sa.core.utils.ListUtils;

/**
 * @author Alessandro Paolillo
 */
@Service
public class PlantService implements IPlantService {

	private static final Logger logger = LoggerFactory.getLogger(PlantService.class.getName());

	@Autowired
	private IBeanPopulate<TPlant, TPlantBean> plantConverter;
	
	@Autowired
	private TPlantDao tPlantDao;
	
	@Autowired
	private TPlantImageDao tPlantImageDao;
	
	@Autowired
	private CoreProperties properties;
	
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public TPlantBean getPlant(Long id) throws Exception {
		
		TPlantBean result = new TPlantBean();
		try {
			
			TPlant entity = tPlantDao.findByPk( id ); 
			if (entity!=null)
				result = this.plantConverter.convertdbToPage(entity);
			logger.info("Query eseguita correttamente");
		} catch(Exception e){
			logger.error("getPlant >>>>>>>>>>>> " + e, e);
			throw(e);
		}
		return result;
	}
	
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public List<TPlantBean> getPlants( ) throws Exception {
		
		List<TPlantBean> resultList = new ArrayList<TPlantBean>();
		try {
			
			List<TPlant> list = tPlantDao.getPlants(  );
			if(ListUtils.listIsNotEmpty(list)) 
				for (TPlant entity : list) 
					resultList.add(this.plantConverter.convertdbToPage(entity));
				
			logger.info("Query eseguita correttamente");
		} catch(Exception e){
			logger.error("getPlants >>>>>>>>>>>> " + e, e);
			throw(e);
		}
		return resultList;
	}
	
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public List<TPlantBean> searchPlant( SearchPlantFilter searchPlantFilter) throws Exception {
		
		List<TPlantBean> resultList = new ArrayList<TPlantBean>();
		try {
			List<TPlant> list = tPlantDao.searchPlant( searchPlantFilter );
			if(ListUtils.listIsNotEmpty(list)) 
				for (TPlant entity : list) {
					TPlantBean bean = this.plantConverter.convertdbToPage(entity);
					TPlantImage entityImage = tPlantImageDao.findByIdPlant( entity.getId() );
					if ( entityImage!=null )
						bean.setUrlImage( properties.PLANT_IMAGE_URL+entityImage.getUrl()+properties.PLANT_IMAGE_EXT );
					resultList.add( bean );
				}
				
			logger.info("Query eseguita correttamente");
		} catch(Exception e){
			logger.error("searchPlant >>>>>>>>>>>> " + e, e);
			throw(e);
		}
		return resultList;
	}
	
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public Long searchCountPlant( SearchPlantFilter searchPlantFilter) throws Exception {
		
		Long searchCountPlantValue = new Long(0);
		try {
			searchCountPlantValue = tPlantDao.searchCountPlant( searchPlantFilter );
				
			logger.info("Query eseguita correttamente");
		} catch(Exception e){
			logger.error("searchCountPlant >>>>>>>>>>>> " + e, e);
			throw(e);
		}
		return searchCountPlantValue;
	}
}
